package com.wondertek.applet.controller.sys;

import com.wondertek.common.base.Result;
import com.wondertek.common.config.UploadConfig;
import com.wondertek.common.log.AutoLog;
import com.wondertek.common.util.security.SecurityUtil;
import com.wondertek.service.dto.app.req.*;
import com.wondertek.service.dto.sys.req.SysUserForgetReq;
import com.wondertek.service.dto.sys.res.SysLoginRes;
import com.wondertek.service.dto.sys.res.SysUserRes;
import com.wondertek.service.entity.business.ConsumerPo;
import com.wondertek.service.entity.sys.AppVersionPo;
import com.wondertek.service.entity.sys.SysConfigPo;
import com.wondertek.service.entity.sys.SysCountryInfoPo;
import com.wondertek.service.entity.sys.SysFilePo;
import com.wondertek.service.service.sys.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @description: app登录
 * @author: qinjianhui
 * @create: 2024-07-16 17:57
 **/
@RestController
@RequestMapping("/app/sys/auth/")
@Tag(name = "app系统登录管理")
@CrossOrigin("*")
public class AuthController {

    @Resource
    private ISysConsumerService consumerService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private ISysFileService sysFileService;

    @Resource
    private UploadConfig uploadConfig;

    @Resource
    private ISysCountryInfoService sysCountryInfoService;

    @Resource
    private ISysUserService userService;

    @Resource
    private IAppVersionService appVersionService;

    @GetMapping("/getCountryInfo")
    @Operation(summary = "获取国家信息")
    public Result<List<SysCountryInfoPo>> getCountryInfo(){
        return Result.success(sysCountryInfoService.list());
    }

    /*
    * 获取用户信息
    * */
    @PostMapping("/get/user")
    @Operation(summary = "获取用户信息")
    public Result<ConsumerPo> mobileRegister(){
        ConsumerPo user = consumerService.getUser();
        if(StringUtils.isNotBlank(user.getAvatar())){
            SysFilePo byId = sysFileService.getById(user.getAvatar());
            user.setViewUrl(uploadConfig.getViewPrefix()+"/"+byId.getFileName());
        }
        return Result.success(user);
    }

    /*
    * 手机号注册
    * */
    @PostMapping("/mobile/register")
    @Operation(summary = "手机号注册")
    public Result<?> mobileRegister(@RequestBody @Validated AppAuthMobileRegisterReq req){
        consumerService.mobileRegister(req);
        return Result.success("register.success");
    }

    /*
     * 短信获取验证码
     * */
    @GetMapping("/massage/code")
    @Operation(summary = "短信获取验证码")
    public Result<?> massageCode(@RequestParam(name="mobile") String mobile) throws Exception {
        consumerService.massageCode(mobile);
        return Result.success("sms.send.success");
    }

    /*
    * 校验短信验证码
    * */
    @PostMapping("/check/massageCode")
    @Operation(summary = "校验短信验证码")
    public Result<?> checkMassageCode(@RequestBody @Validated AppAuthCheckMassageCodeReq req){
        if(consumerService.checkMassageCode(req) || "7777".equals(req.getCode())){
            return Result.success();
        }else {
            return Result.error("captcha.error");
        }
    }

    /*
    * 修改个人信息
    * */
    @PostMapping("/update/info")
    @Operation(summary = "修改个人信息")
    public Result<?> updateInfo(@RequestBody @Validated AppAuthUpdateInfoReq req){
        consumerService.updateInfo(req);
        return Result.success();
    }

    /*
     * 校验邮箱验证码
     * */
    @PostMapping("/check/code")
    @Operation(summary = "校验邮箱验证码")
    public Result<?> checkCode(@RequestBody @Validated AppAuthCheckCodeReq req){
        if("7777".equals(req.getCode())){
            return Result.success();
        }
        if(consumerService.checkCode(req)){
            return Result.success();
        }else {
            return Result.error("captcha.error");
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @Operation(summary = "退出登录")
    @AutoLog(category = "sys", handleType = "log.logout",
            handleNote = "'登出系统:'+@securityUtil.getLoginUser().getUsername()+'账户'")
    public Result<?> logout() {
        String token = SecurityUtil.getLoginUser().getToken();
        consumerService.logout(token);
        return Result.success("user.logout.success");
    }

    /*
    * 邮箱获取验证码
    * */
    @GetMapping("/email/code")
    @Operation(summary = "邮箱获取验证码")
    public Result<?> emailCode(@RequestParam(name="email") String email){
        consumerService.getMailCode(email);
        return Result.success();
    }

    /*
    * 注册接口
    * */
    @PostMapping("/register")
    @Operation(summary = "注册")
    public Result<?> register(@RequestBody @Validated AppAuthRegisterReq appAuthRegisterReq){
        //校验验证码
        consumerService.register(appAuthRegisterReq);
        return Result.success("user.logout.success");
    }

    /*
     * 重置密码
     * */
    @PostMapping("/update/password")
    @Operation(summary = "重置密码")
    public Result<?> restPassword(@RequestBody @Validated AppAuthUpdatePasswordReq passwordReq){
        consumerService.restPassword(passwordReq);
        return Result.success("update.success");
    }


    /*
     * installer重置密码(邮箱)
     * */
    @PostMapping("/update/installer/password")
    @Operation(summary = "installer重置密码(邮箱)")
    public Result<?> restInstallerPassword(@RequestBody @Validated SysUserForgetReq passwordReq){
        userService.forgetAndRestPassword(passwordReq);
        return Result.success("update.success");
    }

    /*
    * 登陆接口
    * */
    @PostMapping("/login")
    @Operation(summary = "登陆")
    @AutoLog(category = "sys", handleType = "log.login",
            handleNote = "'登录系统:'+#loginReq.email+'账户'")
    public Result<SysLoginRes> login(@RequestBody @Validated AppAuthLoginReq loginReq){
        return Result.success(consumerService.login(loginReq));
    }


    /**
     * 更新fcmtoken
     *
     * @param token
     * @return
     */
    @GetMapping("/updateFcmToken")
    @Operation(summary = "更新fcmtoken")
    public Result<?> updateFcmToken(@RequestParam("token") String token){
        consumerService.updateFcmToken(token);
        return Result.success();
    }


    /*
     * 获取安装角色的用户信息
     * */
    @PostMapping("/installer/get/user")
    @Operation(summary = "获取安装角色的用户信息")
    public Result<SysUserRes> getInstallerUserInfo(){
        return Result.success(consumerService.getInstallerUserInfo());
    }


    /**
     * 安装角色重置密码
     *
     * @param passwordReq
     * @return
     */
    @PostMapping("/installer/update/password")
    @Operation(summary = "安装角色重置密码")
    public Result<?> installerRestPassword(@RequestBody @Validated AppAuthInstallerRestPasswordReq passwordReq){
        consumerService.installerRestPassword(passwordReq);
        return Result.success("update.success");
    }

    /**
     * 获取系统配置
     *
     * @return
     */
    @GetMapping("/getConfig")
    @Operation(summary = "获取系统配置")
    public Result<SysConfigPo> getConfig(){
        SysConfigPo sysConfigPo = sysConfigService.getConfig();
        return Result.success(sysConfigPo);
    }

    /**
     * 获取版本更新信息
     *
     * @return
     */
    @GetMapping("/getAppVersion")
    @Operation(summary = "获取版本更新信息")
    public Result<AppVersionPo> getAppVersion(@RequestParam("version") String version,@RequestParam("type") Integer type){
        AppVersionPo appVersionPo = appVersionService.getAppVersion(version, type);
        return Result.success(appVersionPo);
    }
}

